package com.xnyy.lottery.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xnyy.lottery.domain.entity.Region;
import com.xnyy.lottery.domain.mapper.RegionMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @description:
 * @auther: Administrator
 * @date: 2022/9/16 02:15
 */
@AllArgsConstructor
@Service
public class RegionService extends ServiceImpl<RegionMapper, Region> {
    private final RegionMapper regionMapper;

    public List<Region> findAll() {
        return regionMapper.selectList(null);
    }

    @Transactional(rollbackFor = Exception.class)
    public Region add(Region region) {
        Region region1 = this.lambdaQuery()
                .eq(Region::getName, region.getName())
                .oneOpt().orElse(null);
        if (region1 != null) {
            return region1;
        } else {
            regionMapper.insert(region);
        }
        return region;
    }

    public List<String> getAllRegionName() {
        return this.lambdaQuery()
                .select(Region::getName)
                .list()
                .stream().map(Region::getName)
                .collect(Collectors.toList());
    }
}
